﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace LcfContols
{
    public class LTextBox:TextBox
    {
        #region 变量
        /// <summary>
        /// Text中是否是提示信息
        /// </summary>
        private bool _isTip = true;

        /// <summary>
        /// 实际内容
        /// </summary>
        private string textmessage = string.Empty;

        /// <summary>
        /// 是否只允许输入数字
        /// </summary>
        private bool _isNumber = false;

        /// <summary>
        /// 验证的正则表达式
        /// </summary>
        private string _regex = string.Empty;

        //正则验证是否成功
        private bool _validation = true;

        /// <summary>
        /// 提示字符是否为默认支付，请使用Value()方法获取值
        /// </summary>
        public bool TipIsDefualtValue { get; set; }

        //数字输入需要排除的ctrl组合键以及其他排除键 [详细查询Ascii按键表]
        private char[] widthOutkey = new char[] { '\x1', '\x3', '\x16', '\x18', '\x1A', '\x0d', '.', '-', '\x8' };
        #endregion

        #region 属性
        [Description("正则字符串")]
        public string regex
        {
            get
            {
                return _regex;
            }
            set
            {
                _regex = value;
                Validated += Validate;
            }
        }

        public override string Text
        {
            get
            {
                return _validation ? base.Text : string.Empty;
            }
            set
            {
                base.Text = value;
                if (Tip && string.IsNullOrEmpty(textmessage))
                {
                    ForeColor = SystemColors.ScrollBar;
                    textmessage = value;
                }
                else
                    ForeColor = SystemColors.WindowText;
            }
        }

        [Description("Text是否是提示字符")]
        public bool Tip
        {
            get
            {
                return string.IsNullOrEmpty(textmessage)?false:_isTip;
            }
            set
            {
                _isTip = value;
                if (!value)
                {
                    textmessage = string.Empty;
                    ForeColor = SystemColors.WindowText;
                }
            }
        }

        [Description("是否只允许输入数字(包含小数点)")]
        public bool isNumber
        {
            get
            {
                return _isNumber;
            }
            set
            {
                _isNumber = value;
                if (value)
                    KeyPress += Number;
                else
                    KeyPress -= Number;
            }
        }
        #endregion

        public LTextBox(){
            ForeColor = SystemColors.ScrollBar;
        }

        private void Number(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && !widthOutkey.Contains(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void ClearTip(object sender, EventArgs e)
        {
            _validation = true;
            if (_isTip && base.Text == textmessage)
            {
                base.Text = string.Empty;
                _isTip = false;
            }
            ForeColor = SystemColors.WindowText;
        }

        private void LoseFource(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textmessage) && string.IsNullOrWhiteSpace(base.Text))
            {
                ForeColor = SystemColors.ScrollBar;
                base.Text = textmessage;
                _isTip = true;
            }
        }

        /// <summary>
        /// 正则验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Validate(object sender, EventArgs e)
        {
            _validation = true;
            if (!string.IsNullOrEmpty(_regex) && Text != textmessage)
            {
                Regex reg = new Regex(_regex);
                if (!reg.IsMatch(Text))
                {
                    ForeColor = Color.Red;
                    _validation = false;
                }
                else
                {
                    ForeColor = SystemColors.WindowText;
                    _validation = true;
                }
            }
        }

        /// <summary>
        /// 获取值,建议方法，如直接获取Text可能获取到提示字符串
        /// </summary>
        public string Value
        {
            get
            {
                if (_validation && ((base.Text == textmessage && TipIsDefualtValue) || base.Text != textmessage))
                {
                    return base.Text;
                }
                return string.Empty;
            }
        }

    }
}
